<?php

namespace App\Service\Common;


use App\Service\OpenShop\Feishu\BaseService;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use Exception;
use Illuminate\Support\Str;

/**
 * 二维码
 */
class MakeQRCodeService extends BaseService
{

    const FILE_NAME_PREFIX_CHECK = 'kuafu_check_qrcode';

    public string $label    = '';      //二维码下面的文字描述
    public string $logo     = '';      //二维码中间的logo
    public int    $logoSize = 0;       //logo大小   推荐值：50
    public int    $size     = 0;       //二维码大小  推荐值：300
    public int    $margin   = 0;       //二维码边距  推荐值：10

    /**
     * 生成稽核人员二维码
     * @param string $content
     * @throws Exception
     */
    public function makeCheckQRCode(string $content): array
    {
        $this->logo     = public_path('images/kxfLogo.png');
        $this->logoSize = 60;
        $this->size     = 300;
        $this->margin   = 5;
        return $this->baseQRCode($content);
    }


    /**
     * 制作二维码
     * @param string $content
     * @return array
     * @throws Exception
     */
    public function baseQRCode(string $content = ''): array
    {
        if (!$this->size || !$this->margin) {  // 大小边距必填·
            return [];
        }
        $writer = new PngWriter();
        $qrCode = QrCode::create($content)
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize($this->size)//大小
            ->setMargin($this->margin)//边距
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelHigh())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));

        //其他样式处理
        if ($this->logo && $this->logoSize && $this->label) {
            $result = $writer->write(
                $qrCode,
                Logo::create($this->logo)->setResizeToWidth($this->logoSize),  // logo.png
                Label::create($this->label)->setTextColor(new Color(0, 0, 0))  // label 文字
            );
        } elseif ($this->logo && $this->logoSize) {
            $result = $writer->write(
                $qrCode,
                Logo::create($this->logo)->setResizeToWidth($this->logoSize)
            );
        } elseif ($this->label) {
            $result = $writer->write(
                $qrCode,
                null,
                Label::create($this->label)->setTextColor(new Color(0, 0, 0))
            );
        } else {
            $result = $writer->write(
                $qrCode
            );
        }
        //header('Content-Type: '.$result->getMimeType());
        $result->getString();
        $file = sys_get_temp_dir() . '/' . self::FILE_NAME_PREFIX_CHECK . Str::random(10) . '.png';
        $result->saveToFile($file);   //获得文件(只会创建文件  不支持创建目录)
        $arr = OssService::uploadFile($file);
        @unlink($file);
        return $arr;
    }
}
